package com.uchedao.vehicledetection.activity;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.AsyncTask;
import android.view.View;
import android.widget.ListView;

import com.nostra13.universalimageloader.utils.L;
import com.uchedao.mybaselibrary.view.dialog_for_ios.ZAlertDialog;
import com.uchedao.vehicledetection.R;
import com.uchedao.vehicledetection.activity.blue_tooth.CJDBaseHttpActivity;
import com.uchedao.vehicledetection.adapter.DraftAdapter;
import com.uchedao.vehicledetection.adapter.DraftAdapter.DraftListener;
import com.uchedao.vehicledetection.global.GlobalInfo;
import com.uchedao.vehicledetection.model.Draft;
import com.uchedao.vehicledetection.network.local.GetDraftListTask;
import com.uchedao.vehicledetection.utils.Util;

import java.util.ArrayList;

/**
 * 草稿列表
 */
public class DraftsActivity extends CJDBaseHttpActivity {

    private ListView lvSource;

    private ArrayList<Draft> listData;

    private DraftAdapter mAadapter;

    @Override
    protected String getActivityTag() {
        return "DraftsActivity";
    }

    @Override
    protected int getLayoutResID() {
        return R.layout.activity_drafts;
    }

    @Override
    protected void initView() {
        lvSource = (ListView) findViewById(R.id.lvDrafts);
    }

    @Override
    protected void initData() {
        setTitle("草稿箱");

//        listData = GlobalInfo.dbHelper.getAllDraft();
        mAadapter = new DraftAdapter(this);
        mAadapter.setDraftListener(draftListener);
        lvSource.setAdapter(mAadapter);

    }


    @Override
    protected void initListener() {

        firstdata();
    }

    @Override
    protected void onResume() {
        super.onResume();
        L.i("onResume");
//        listData = GlobalInfo.dbHelper.getAllDraft();
//        if (mAadapter != null) {
//            mAadapter.setList(listData);
//        }
        loadData(0);//刷新
    }


    private DraftListener draftListener = new DraftListener() {

        @Override
        public void onItemClick(int action, final int position) {
            switch (action) {
                case DraftAdapter.ACTION_OPEN:
                case DraftAdapter.ACTION_EDIT:
                    GlobalInfo.draft = listData.get(position);
                    // 启动检测页面
                    startCheckActivity();
                    break;

                case DraftAdapter.ACTION_DELETE:

                    ZAlertDialog.Builder dialog = new ZAlertDialog.Builder(DraftsActivity.this);

//                    CustomDialog dialog = new CustomDialog(DraftActivity.this);
                    dialog.setTitle(R.string.delete_draft);
                    dialog.setMessage(R.string.delete_draft_message);
                    dialog.setPositiveButton(R.string.ok, new android.content.DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            Draft draft = listData.get(position);
                            // 从列表中移除
                            listData.remove(position);
                            mAadapter.notifyDataSetChanged();
                            // 从数据库中删除
                            GlobalInfo.dbHelper.deleteDraf(draft.car_plate);
                            loadData(0);

                        }
                    });
                    dialog.setNegativeButton(R.string.cancel, null);
                    dialog.show();
                    break;
            }

        }
    };

    /**
     * 启动检测页面(主页面)
     */
    private void startCheckActivity() {
        Intent checkIntent = new Intent(this, MainActivity.class);
        // checkIntent.putExtra("car_plate", carPlate);
        startActivity(checkIntent);
    }

    @Override
    public View getEmptyView(String msg) {
        return super.getEmptyView();
    }

    @Override
    public View getContentView() {
        return lvSource;
    }

    @Override
    public void invalidateViews() {

    }

    @Override
    public void loadData(int page) {
        onLoadStart();
//        ProgressDialogUtil.showProgressDlg(DraftsActivity.this, "数据加载中");
        GetDraftListTask task = new GetDraftListTask(this, new GetDraftListTask.IResultListener() {
            @Override
            public void onResult(boolean success, String error, ArrayList<Draft> draftList) {

//                ProgressDialogUtil.dismissProgressDlg();

                if (success) {
                    listData = draftList;
                    mAadapter.setList(listData);
//                    System.out.println("ws----draftList.size = "+draftList.size());
                    if (draftList != null && draftList.size() > 0) {
                        onLoadSuccess();
                    } else {
                        onLoadEmpty();
                    }
                } else {
                    onLoadFailed();
                }

            }
        });

        if (Util.hasHoneycomb()) {
            task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
        } else {
            task.execute();
        }
    }

}
